<template>
  <div>
    <PageWrapper :contentFullHeight="true" :contentBackground="true">
      <BasicTable @register="registerTable">
        <template #toolbar>
          <a-button type="primary" @click="handleCreate"> {{ t('common.create') }} </a-button>
        </template>
        <template #action="{ record }">
          <TableAction
            :actions="[
              {
                icon: 'clarity:note-edit-line',
                onClick: handleEdit.bind(null, record),
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                popConfirm: {
                  title: t('common.confirmDelete'),
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </BasicTable>
      <!-- 计数器弹窗 -->
      <CountSettingModal @register="registerCountSettingModal" @success="reload" />
    </PageWrapper>
  </div>
</template>

<script setup lang="ts">
  import { useI18n } from '/@/hooks/web/useI18n';
  import { PageWrapper } from '/@/components/Page';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';

  import { getCodeTypeList } from '/@/api/demo/system';

  import CountSettingModal from './CountSettingModal.vue';
  import { getCounterSettingColumns } from './data';
  import { deleteCodeTypeList } from '/@/api/demo/system';

  const { t } = useI18n();
  const { createMessage } = useMessage();

  // 新增
  const handleCreate = () => {
    openCountSettingModal(true, { isUpdate: false });
  };

  // 修改
  const handleEdit = (record: Recordable) => {
    openCountSettingModal(true, { isUpdate: true, record: record });
  };

  // 删除
  const handleDelete = (record: Recordable) => {
    deleteCodeTypeList(record.counterID, record.remark, record.mdList).then(() => {
      createMessage.success(t('common.delSuccessText'));
      reload();
    });
  };

  // 初始化表格
  const [registerTable, { reload }] = useTable({
    api: getCodeTypeList,
    columns: getCounterSettingColumns(),
    showTableSetting: true,
    bordered: true,
    showIndexColumn: false,
    canResize: false,
    actionColumn: {
      width: 80,
      title: t('common.action'),
      dataIndex: 'action',
      slots: { customRender: 'action' },
    },
  });

  // 计数器弹窗
  const [registerCountSettingModal, { openModal: openCountSettingModal }] = useModal();
</script>
